{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "import pandas as pd"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "     年份  平台   销量\n0  2019  京东  100\n1  2019  淘宝  200\n2  2020  京东  300\n3  2020  淘宝  400",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年份</th>\n      <th>平台</th>\n      <th>销量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2019</td>\n      <td>京东</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2019</td>\n      <td>淘宝</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2020</td>\n      <td>京东</td>\n      <td>300</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2020</td>\n      <td>淘宝</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({\n",
    "    '年份': [2019, 2019, 2020, 2020],\n",
    "    '平台': ['京东', '淘宝', '京东', '淘宝'],\n",
    "    '销量': [100, 200, 300, 400],\n",
    "})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "平台     京东   淘宝\n年份            \n2019  100  200\n2020  300  400",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>平台</th>\n      <th>京东</th>\n      <th>淘宝</th>\n    </tr>\n    <tr>\n      <th>年份</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2019</th>\n      <td>100</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2020</th>\n      <td>300</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot(df,\n",
    "         index='年份',\n",
    "         columns='平台',\n",
    "         values='销量')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "     年份  平台   销量\n0  2019  京东  100\n1  2019  淘宝  200\n2  2020  京东  300\n3  2020  淘宝  400\n4  2020  淘宝  500",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>年份</th>\n      <th>平台</th>\n      <th>销量</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>2019</td>\n      <td>京东</td>\n      <td>100</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2019</td>\n      <td>淘宝</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2020</td>\n      <td>京东</td>\n      <td>300</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>2020</td>\n      <td>淘宝</td>\n      <td>400</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2020</td>\n      <td>淘宝</td>\n      <td>500</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1 = pd.DataFrame({\n",
    "    '年份': [2019, 2019, 2020, 2020, 2020],\n",
    "    '平台': ['京东', '淘宝', '京东', '淘宝', '淘宝'],\n",
    "    '销量': [100, 200, 300, 400, 500],\n",
    "})\n",
    "df1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "Index contains duplicate entries, cannot reshape",
     "output_type": "error",
     "traceback": [
      "\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[0;31mValueError\u001B[0m                                Traceback (most recent call last)",
      "\u001B[0;32m<ipython-input-10-74e514775930>\u001B[0m in \u001B[0;36m<module>\u001B[0;34m\u001B[0m\n\u001B[1;32m      2\u001B[0m          \u001B[0mindex\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0;34m'年份'\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m      3\u001B[0m          \u001B[0mcolumns\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0;34m'平台'\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m----> 4\u001B[0;31m          values='销量')\n\u001B[0m",
      "\u001B[0;32m~/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/reshape/pivot.py\u001B[0m in \u001B[0;36mpivot\u001B[0;34m(data, index, columns, values)\u001B[0m\n\u001B[1;32m    448\u001B[0m         \u001B[0;32melse\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    449\u001B[0m             \u001B[0mindexed\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mdata\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_constructor_sliced\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mdata\u001B[0m\u001B[0;34m[\u001B[0m\u001B[0mvalues\u001B[0m\u001B[0;34m]\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mvalues\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mindex\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0mindex\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 450\u001B[0;31m     \u001B[0;32mreturn\u001B[0m \u001B[0mindexed\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0munstack\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mcolumns\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m    451\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    452\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/series.py\u001B[0m in \u001B[0;36munstack\u001B[0;34m(self, level, fill_value)\u001B[0m\n\u001B[1;32m   3548\u001B[0m         \u001B[0;32mfrom\u001B[0m \u001B[0mpandas\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mcore\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mreshape\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mreshape\u001B[0m \u001B[0;32mimport\u001B[0m \u001B[0munstack\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m   3549\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m-> 3550\u001B[0;31m         \u001B[0;32mreturn\u001B[0m \u001B[0munstack\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mself\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mlevel\u001B[0m\u001B[0;34m,\u001B[0m \u001B[0mfill_value\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m   3551\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m   3552\u001B[0m     \u001B[0;31m# ----------------------------------------------------------------------\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001B[0m in \u001B[0;36munstack\u001B[0;34m(obj, level, fill_value)\u001B[0m\n\u001B[1;32m    417\u001B[0m             \u001B[0mlevel\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0mlevel\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    418\u001B[0m             \u001B[0mfill_value\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0mfill_value\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 419\u001B[0;31m             \u001B[0mconstructor\u001B[0m\u001B[0;34m=\u001B[0m\u001B[0mobj\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_constructor_expanddim\u001B[0m\u001B[0;34m,\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m    420\u001B[0m         )\n\u001B[1;32m    421\u001B[0m         \u001B[0;32mreturn\u001B[0m \u001B[0munstacker\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mget_result\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001B[0m in \u001B[0;36m__init__\u001B[0;34m(self, values, index, level, value_columns, fill_value, constructor)\u001B[0m\n\u001B[1;32m    139\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    140\u001B[0m         \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_make_sorted_values_labels\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 141\u001B[0;31m         \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0m_make_selectors\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m    142\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    143\u001B[0m     \u001B[0;32mdef\u001B[0m \u001B[0m_make_sorted_values_labels\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mself\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;32m~/anaconda3/envs/pandas与办公自动化/lib/python3.7/site-packages/pandas/core/reshape/reshape.py\u001B[0m in \u001B[0;36m_make_selectors\u001B[0;34m(self)\u001B[0m\n\u001B[1;32m    177\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    178\u001B[0m         \u001B[0;32mif\u001B[0m \u001B[0mmask\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0msum\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m)\u001B[0m \u001B[0;34m<\u001B[0m \u001B[0mlen\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mindex\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m:\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0;32m--> 179\u001B[0;31m             \u001B[0;32mraise\u001B[0m \u001B[0mValueError\u001B[0m\u001B[0;34m(\u001B[0m\u001B[0;34m\"Index contains duplicate entries, cannot reshape\"\u001B[0m\u001B[0;34m)\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n\u001B[0m\u001B[1;32m    180\u001B[0m \u001B[0;34m\u001B[0m\u001B[0m\n\u001B[1;32m    181\u001B[0m         \u001B[0mself\u001B[0m\u001B[0;34m.\u001B[0m\u001B[0mgroup_index\u001B[0m \u001B[0;34m=\u001B[0m \u001B[0mcomp_index\u001B[0m\u001B[0;34m\u001B[0m\u001B[0;34m\u001B[0m\u001B[0m\n",
      "\u001B[0;31mValueError\u001B[0m: Index contains duplicate entries, cannot reshape"
     ]
    }
   ],
   "source": [
    "pd.pivot(df1,\n",
    "         index='年份',\n",
    "         columns='平台',\n",
    "         values='销量')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "平台     京东   淘宝\n年份            \n2019  100  200\n2020  300  400",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>平台</th>\n      <th>京东</th>\n      <th>淘宝</th>\n    </tr>\n    <tr>\n      <th>年份</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2019</th>\n      <td>100</td>\n      <td>200</td>\n    </tr>\n    <tr>\n      <th>2020</th>\n      <td>300</td>\n      <td>400</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.pivot_table(df1,\n",
    "         index='年份',\n",
    "         columns='平台',\n",
    "         values='销量',\n",
    "        aggfunc='min')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}